﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Web.Services;
using System.Web.Script.Services;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var User = Membership.GetUser();
            if (User != null)
            {
                messages.Disabled = false;
                messages.Visible = true;
                MysiteTopnavUrl.Visible = true;
                SearchsiteTopnavUrl.Visible = true;
                CreatedateTopnavUrl.Visible = true;

            }
        }
    }

    [WebMethod]
    public static Boolean loggedIn()
    {
        var User = Membership.GetUser();
        if (User != null)
        {
            return true;
            }
        else
        {
            return false;          
        }
    }

    public void loadMessageCenter()
    {
        LoadControl("~/UC/MessageCenterPopup.ascx");
    }

    private int checkForMessages(string userName)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        var ListOfBeskeder = from m in db.Messages
                             from c in db.Conversations
                             where c.aspnet_User.UserName == userName || c.Datenight.aspnet_User.UserName == userName
                             where m.conversationId == c.conversationId
                             where m.isRead == false
                             where m.userName != userName
                             select new { m.MessageID };


        return ListOfBeskeder.ToList().Count();
    }

    protected void populateDatabase(object sender, EventArgs e)
    {
        int i = 0;
       
            Random ran = new Random();
            DateiaftenDataContext db = new DateiaftenDataContext();
            var listOfUsers = from u in db.aspnet_Users
                              select new { u.UserName };

            
            while (i < 550)
            {
                Datenight d = new Datenight();
                DateTime dt = new DateTime(2013, 10, ran.Next(7, 11), 12, 12, 12, 12);
                d.DatePlace = ran.Next(1, 1422);
                d.Date = dt;
                d.Description = "Dette er en auto generet date, derfor har den noget fast tekst, fordi det i virkeligheden ikke betyder særlig meget om hvad for noget tekst her står";
                d.WhoPay = "delt";
                d.Agemin = 20;
                d.Agemax = 40;
                d.Username = listOfUsers.ToList().ElementAt(ran.Next(0, listOfUsers.ToList().Count())).UserName;
                d.GenderId = ran.Next(1, 3);
                i++;
                db.Datenights.InsertOnSubmit(d);
                try
                {
                    db.SubmitChanges();

                }
                catch (Exception)
                {
                    db.Datenights.DeleteOnSubmit(d);
                    
                }
                finally
                {
                    db.SubmitChanges();
                }
            }
    }
    protected void CreateEditDateLLBTM_Click(object sender, EventArgs e)
    {
        DateiaftenDataContext db = new DateiaftenDataContext();
        var currentUser = db.aspnet_Users.SingleOrDefault(x => x.UserName == Membership.GetUser().UserName);
        var datenight = from u in db.aspnet_Users
                        from d in db.Datenights
                        where d.Username == currentUser.UserName
                        where d.Date.CompareTo(DateTime.Today) > 0
                        select d;


        if (datenight.Count() >= 1)
        {
            Page.Response.Redirect("~/Restricted/EditDateSite.aspx");
        }
        else
        {
            Page.Response.Redirect("~/Restricted/CreatedateSite.aspx");
        }
    }
}
